<script>
const xStore  = new Vuex.Store({
    state: {
        currentTallyId: 0,
        buyTallyShow: false,
        dayDialog: false,
        currentTally: {},
        industryOptions: [],
        userData: {},
        tallyList: [],
        categoryListIn: [],
        categoryListOut: [],
        accountList: [],
        customList: [],
        day:0,
        gysList: [],
        memberList: [],
        projectList: [],
        accountTags: [],
        materialList:[],
        loginToken: localStorage.getItem('loginToken') || '',
        stockWareHouseList:[], // 前100仓库数据
        stockProductList:[], // 前100产品数据
    },
    mutations: {
        setUserData (state, newVal) {
            state.userData = newVal
        },
        setTallyId (state, newId) {
            state.clientId = newId
        },
        setTally(state, tallyInfo) {
            state.currentTally = tallyInfo
            state.currentTallyId = tallyInfo['id']
        },
        setTallyList (state, newVal) {
            state.tallyList = newVal
        },
        setCategoryListIn (state, newVal) {
            state.categoryListIn = newVal
        },
        setCategoryListOut (state, newVal) {
            state.categoryListOut = newVal
        },
        setAccountList (state, newVal) {
            state.accountList = newVal
        },
        setCustomList (state, newVal) {
            state.customList = newVal
        },
        setMemberList (state, newVal) {
            state.memberList = newVal
        },
        setProjectList (state, newVal) {
            state.projectList = newVal
        },
        setGysList(state, newVal) {
            state.gysList = newVal
        },
        setAccountTags(state, newVal) {
            state.accountTags = newVal
        },
        setIndustry(state, indVal) {
            state.industryOptions = indVal
        }
    },
    actions: {
        getAccountList({ commit, dispatch, state }, tallyId) {
            return new Promise(
                function (resolve, reject) {
                    axios.get('/api/account/lists?tally_id='+tallyId,{
                        method: 'get',
                        headers: {'token': loginToken},
                    }).then(function(res) {
                        if(res.data.status === 'success'){
                            state.accountList = res.data.data
                            resolve(res.data)
                        }else{
                            reject(res)
                        }
                    })
                }
            );
        },

        checkTallyById({ commit, dispatch, state },tally_id = 0){
            return new Promise(
                function (resolve, reject) {
                    axios.post('/api/tally/check-tally',{tally_id:tally_id},{
                        headers: {'token': loginToken},
                    }).then(function(res) {
                        console.log("-=-=====失败")
                        state.day = res.data['data']['day']
                        if( state.day  <= 7){
                            state.dayDialog = true
                        }
                        state.currentTally.express_time = res.data['data']['express_time']
                        resolve(res.data)
                    })
                }
            );
        },
        checkTally({ commit, dispatch, state }){
            return new Promise(
                function (resolve, reject) {
                    axios.post('/api/tally/check-tally',{tally_id:currentTallyId},{
                        headers: {'token': loginToken},
                    }).then(function(res) {
                        state.day = res.data['data']['day']
                        if( state.day  <= 7){
                            state.dayDialog = true
                        }
                        state.currentTally.express_time = res.data['data']['express_time']
                        resolve(res.data)
                    })
                }
            );
        },
        
        getAccountTags({ commit, dispatch, state }, tallyId){
            return new Promise(
                function (resolve, reject) {
                    axios.get('/api/account/tags?tally_id='+tallyId,{
                        method: 'get',
                        headers: {'token': loginToken},
                    }).then(function(res) {
                        if(res.data.status === 'success'){
                            state.accountTags = res.data.data
                            resolve(res.data)
                        }else{
                            reject(res)
                        }
                    })
                }
            )
        },
        getIndustryOptions({ commit, dispatch, state }){
            return new Promise(
                function (resolve, reject) {
                    axios.get('/api/industry/lists',{
                        method: 'get',
                        headers: {'token': loginToken},
                    }).then(function(res) {
                        if(res.data.status === 'success'){
                            state.industryOptions = res.data.data
                            resolve(res.data)
                        }else{
                            reject(res)
                        }
                    })
                }
            )
        },
        getCategoryListBase({ commit, dispatch, state }, actInfo) {
            const tallyId = actInfo['tallyId']
            const cateType = actInfo['cateType']
            return new Promise(function(resolve, reject){
                axios.get('/api/category/lists?tally_id='+tallyId+'&type='+cateType,{
                    method: 'get',
                    headers: {'token': loginToken},
                }).then(function(res) {
                    if(res.data.status === 'success'){
                        if(cateType === 1) {
                            state.categoryListIn = res.data.data
                        }else if(cateType === 2) {
                            state.categoryListOut = res.data.data
                        }
                        resolve(res.data)
                    }else{
                        reject(res)
                    }
                })
            })
        },
        getProjectList({ commit, dispatch, state }, tallyId) {
            return new Promise(
                function (resolve, reject) {
                    axios.get('/api/project/lists?tally_id='+tallyId,{
                        method: 'get',
                        headers: {'token': loginToken},
                    }).then(function(res) {
                        if(res.data.status === 'success'){
                            state.projectList = res.data.data
                            resolve(res.data)
                        }else{
                            reject(res.data)
                        }
                    })
                }
            )
        },
        getMaterialList({ commit, dispatch, state }, tallyId) {
            return new Promise(
                function (resolve, reject) {
                    axios.get('/api/material/lists-all?tally_id='+tallyId,{
                        method: 'get',
                        headers: {'token': loginToken},
                    }).then(function(res) {
                        if(res.data.status === 'success'){
                            state.materialList = res.data.data
                            resolve(res.data)
                        }else{
                            reject(res.data)
                        }
                    })
                }
            )
        },
        getMemberList({ commit, dispatch, state }, tallyId) {
            return new Promise(
                function (resolve, reject) {
                    axios.get('/api/member/lists?tally_id='+tallyId,{
                        method: 'get',
                        headers: {'token': loginToken},
                    }).then(function(res) {
                        if(res.data.status === 'success'){
                            state.memberList = res.data.data
                            resolve(res.data)
                        }else{
                            reject(res.data)
                        }
                    })
                }
            )
        },
        getStoreList({ commit, dispatch, state }, actInfo){
            const tallyId = actInfo['tallyId']
            const cateType = actInfo['type']
            return new Promise(
                function (resolve, reject) {
                    axios.get('/api/store/lists?type='+cateType+'&tally_id='+tallyId,{
                        method: 'get',
                        headers: {'token': loginToken},
                    }).then(function(res) {
                        if(res.data.status === 'success'){
                            if(cateType === 1) {
                                state.customList = res.data.data
                            }else if(cateType === 2) {
                                state.gysList = res.data.data
                            }
                            resolve(res.data)
                        }else{
                            reject(res.data)
                        }
                    })
                }
            )
        },
        getTallyList({ commit, dispatch, state }){
            return new Promise(
                function (resolve, reject) {
                    axios.get('/api/tally/lists',{
                        method: 'get',
                        headers: {'token': loginToken},
                    }).then(function(res) {
                        if(res.data.status === 'success'){
                            state.tallyList = res.data.data
                            resolve(res.data.data)
                        }else{
                            reject(res.data)
                        }
                    })
                }
            )
        },
        getStockWareHouseList({ commit, dispatch, state }, tallyId){
            return new Promise(
                function (resolve, reject) {
                    axios.get('/api/stock-warehouse/all?tally_id='+tallyId,{
                        method: 'get',
                        headers: {'token': loginToken},
                    }).then(function(res) {
                        if(res.data.status === 'success'){
                            state.stockWareHouseList = res.data.data
                            resolve(res.data.data)
                        }else{
                            reject(res.data)
                        }
                    })
                }
            )
        },
        getStockProductList({ commit, dispatch, state }, tallyId){
            return new Promise(
                function (resolve, reject) {
                    axios.get('/api/stock-product/all?tally_id='+tallyId,{
                        method: 'get',
                        headers: {'token': loginToken},
                    }).then(function(res) {
                        if(res.data.status === 'success'){
                            state.stockProductList = res.data.data
                            resolve(res.data.data)
                        }else{
                            reject(res.data)
                        }
                    })
                }
            )
        },
    }
})
</script>
